package com.xwt.executor;

import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Stc implements Runnable {

    int dpi;
    String imagePDFName;
    String imgFolderPath;
    PDFRenderer renderer;
    int startPages;
    int endPages;


    public Stc(int dpi, String imagePDFName, String imgFolderPath, PDFRenderer renderer, int startPages, int endPages) {
        this.dpi = dpi;
        this.imagePDFName = imagePDFName;
        this.imgFolderPath = imgFolderPath;
        this.renderer = renderer;
        this.startPages = startPages;
        this.endPages = endPages;
    }

    public void run() {
        try {
            doImage(this.dpi,this.imagePDFName,this.imgFolderPath,this.renderer,this.startPages,this.endPages);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void doImage(int dpi, String imagePDFName, String imgFolderPath, PDFRenderer renderer, int startPages, int endPages) throws IOException {
        StringBuffer imgFilePath;
        for (int i = startPages; i < endPages; i++) {
            String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
            imgFilePath = new StringBuffer();
            imgFilePath.append(imgFilePathPrefix);
            imgFilePath.append("_");
            imgFilePath.append(String.valueOf(i + 1));
            imgFilePath.append(".png");
            File dstFile = new File(imgFilePath.toString());
            BufferedImage image = renderer.renderImageWithDPI(i, dpi);
            ImageIO.write(image, "png", dstFile);
        }
    }


}
